<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="isEditRef ? '编辑用户' : '新建用户'"
      width="30%"
      center
    >
      <div class="form">
        <el-form :model="formData" label-width="80px" size="large">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="真实姓名" prop="realname">
            <el-input v-model="formData.realname" placeholder="请输入真实姓名" />
          </el-form-item>
          <el-form-item label="密码" prop="password" v-if="!isEditRef">
            <el-input v-model="formData.password" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item label="手机号码" prop="cellphone">
            <el-input v-model="formData.cellphone" placeholder="请输入手机号码" />
          </el-form-item>
          <el-form-item label="选择角色" prop="roleId">
            <el-select v-model="formData.roleId" placeholder="请选择角色">
              <template v-for="item in entireRoles" :key="item.id">
                <el-option :label="item.name" :value="item.id"></el-option>
              </template>
            </el-select>
          </el-form-item>
          <el-form-item label="选择部门" prop="departmentId">
            <el-select v-model="formData.departmentId" placeholder="请选择部门">
              <template v-for="item in entireDepartments" :key="item.id">
                <el-option :label="item.name" :value="item.id"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmitBtnClick">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import useMainStore from '@/store/main/main'
import useSystemStore from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { reactive, ref } from 'vue'

const emit = defineEmits(['ok'])

const dialogVisible = ref(false)
const formData = reactive<any>({
  id: undefined,
  name: undefined,
  realname: undefined,
  password: undefined,
  cellphone: undefined,
  roleId: undefined,
  departmentId: undefined
})

// 当前页面是否为编辑页面
const isEditRef = ref(false)

function setModalVisible(isEdit: boolean, itemData?: any) {
  dialogVisible.value = true
  isEditRef.value = isEdit

  // 新建数据
  if (isEdit && itemData) {
    for (const key in formData) {
      formData[key] = itemData[key]
    }
  } else {
    // 编辑数据
    for (const key in formData) {
      formData[key] = undefined
    }
  }
}

const mainStore = useMainStore()
const { entireRoles, entireDepartments } = storeToRefs(mainStore)

const systemStore = useSystemStore()
// 确定提交
async function handleSubmitBtnClick() {
  if (isEditRef.value) {
    // 编辑用户
    await systemStore.updateUserAction(formData.id, formData)
  } else {
    // 创建用户
    await systemStore.createUserAction(formData)
  }

  emit('ok')
  dialogVisible.value = false
}

defineExpose({
  setModalVisible
})
</script>

<style lang="less" scoped>
.form {
  padding: 0 20px;
}
</style>
